
var ratio = 80/80;
var jcrop_api;

function initJcrop() {
	jcrop_api = $.Jcrop('#cropbox');
	jcrop_api.setOptions({
		aspectRatio: ratio,
		minSize: [80,80],
		onSelect: updateCoords
	});
}

function nothing(e) {
	e.stopPropagation();
	e.preventDefault();
	return false;
}

function cropboxLoad() {
	initJcrop();
	setSelection();
	return false;
	//return nothing(e);
}

function setSelection() {
	var dim = jcrop_api.getBounds();
	if (dim[0]/dim[1] > ratio) {
		var xc = dim[0]/2,
			w2 = dim[1]*ratio/2;
		jcrop_api.setSelect([Math.round(xc-w2), 0, Math.round(xc+w2), dim[1]]);
		jQuery('#x').val(Math.round(xc-w2));
		jQuery('#y').val(0);
		jQuery('#w').val(Math.round(xc+w2));
		jQuery('#h').val(dim[1]);
	} else {
		var yc = dim[1]/2,
			h2 = dim[0]/(ratio*2);
		jcrop_api.setSelect([0, Math.round(yc-h2), dim[0], Math.round(yc+h2)]);
		jQuery('#x').val(0);
		jQuery('#y').val(Math.round(yc-h2));
		jQuery('#w').val(dim[0]);
		jQuery('#h').val(Math.round(yc+h2));
	}
}

function updateCoords(c) {
	jQuery('#x').val(c.x);
	jQuery('#y').val(c.y);
	jQuery('#w').val(c.w);
	jQuery('#h').val(c.h);
}

function checkCoords() {
	if (parseInt(jQuery('#w').val())>0) return true;
	alert(Dictionary.translate('lang_footballer_photo_select_region'));
	//alert('Please select a crop region then press submit.');
	return false;
}

function destroyCrop() {
	jcrop_api.destroy();
	return false;
	//return nothing(e);
}

$(window).load(cropboxLoad());
